package t2.client;

import java.util.ArrayList;
import java.util.List;

import t2.client.events.LocalATcmdEvent;
import t2.client.events.LocalATcmdEventHandler;
import t2.client.rpc.LocalATcmd;
import t2.client.rpc.LocalATcmdAsync;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;



 /* http://turbomanage.wordpress.com/2010/07/16/dispatchqueue/ */
public class XBeePanel extends VerticalPanel{

    /* TODO: put these in a class. */
    static final int INIT_BEGIN = 0;
    static final int INIT_COMPLETE = 1;
    static final int INIT_FAILURE = -1;

    /* Constructor */
    public XBeePanel() {
        super();

        add(imgXBee);

        /* Listen in on the event bus to receive commands from main and process them. */
        T2.EVENT_BUS.addHandler(LocalATcmdEvent.TYPE, new LocalATcmdEventHandler(){

            @Override
            public void processATcmdEvent(final LocalATcmdEvent event) {

                if(!event.isResponse()){
                    atCmdService.atcmd(event.getCommand(),new AsyncCallback<String>(){

                        @Override
                        public void onFailure(Throwable caught) {
                        }


                        /* Define here how you want to process the return value of each command. */
                        @Override
                        public void onSuccess(String result) {
                            add(decoratorPanel);


                            /* Switch statements on Strings not allowed by Java. */
                            if(event.getCommand() == "NI"){

                                addRow("Name");

                                /* Change the value of the DisclosurePanel header value based on the value in the
                                 * EditableLabel 'name' field. */
                                rows.get(num_rows-1).getEditableLabel().addValueChangeHandler(new ValueChangeHandler <String> (){
                                    @Override
                                    public void onValueChange(ValueChangeEvent<String> event) {
                                        getDisclosurePanel().getHeaderTextAccessor().setText(event.getValue());
                                    }
                                });

                                rows.get(num_rows-1).getEditableLabel().setValue(result, true);
                                T2.EVENT_BUS.fireEvent(new LocalATcmdEvent("", true));


                            } else if (event.getCommand() == "SL"){
                                addRow("ID");
                                rows.get(num_rows-1).getEditableLabel().setValue(result, true);
                                T2.EVENT_BUS.fireEvent(new LocalATcmdEvent("", true));


                            } else if (event.getCommand() == "PL") {
                                addRow("Power Level");
                                rows.get(num_rows-1).getEditableLabel().setValue(result, true);
                                T2.EVENT_BUS.fireEvent(new LocalATcmdEvent("", true));


                            } else if (event.getCommand() == "NC" ) {
                                addRow("Num Child Avail");
                                rows.get(num_rows-1).getEditableLabel().setValue(result, true);
                                T2.EVENT_BUS.fireEvent(new LocalATcmdEvent("", true));
                            }
                        }
                    });
                }
            }
        });
    }



    /* The RPC comms service to the XBee. */
    final LocalATcmdAsync atCmdService = (LocalATcmdAsync) GWT.create(LocalATcmd.class);



    /* Use class 'Resources' for optimization of local image caching. */
    Resources resources = GWT.create(Resources.class);
    Image imgXBee = new Image(resources.xbee());
    final private List <Row> rows = new ArrayList<Row>();
    private DecoratorPanel decoratorPanel = createDataPanel();
    private DisclosurePanel disclosurePanel;
    private Grid dataGrid;
    int num_rows = 0;



    /* Make extra room in the Grid for another row, then add your row, then
     * do the necessary things to the Grid to make it show up. */
    public void addRow(String fieldLabel){
        getDataGrid().resizeRows(++num_rows);
        getRows().add(new Row(fieldLabel));
        getDataGrid().setHTML(num_rows-1, 0, getRows().get(num_rows-1).getFieldLabel());
        getDataGrid().setWidget(num_rows-1, 1, getRows().get(num_rows-1).getEditableLabel());
    }



    /* Make a text box to hold each XBee's data (name, ID, parent) */
    public DecoratorPanel createDataPanel(){
        DecoratorPanel decPanel = new DecoratorPanel();
        FlexTable flexTable = new FlexTable();
        FlexCellFormatter cellFormatter = flexTable.getFlexCellFormatter();


        /* Nestle the elements in their appropriate containers. */
        flexTable.setWidget(0,0,getDisclosurePanel());
        cellFormatter.setColSpan(0,0,1);
        getDisclosurePanel().setAnimationEnabled(true);
        getDisclosurePanel().ensureDebugId("cwDisclosurePanel");
        getDisclosurePanel().setContent(getDataGrid());


        decPanel.setWidget(flexTable);
        return decPanel;
    }




    public List<Row> getRows() {
        return rows;
    }
    public DecoratorPanel getDecoratorPanel() {
        return decoratorPanel;
    }
    public void setDecoratorPanel(DecoratorPanel decoratorPanel) {
        this.decoratorPanel = decoratorPanel;
    }
    public Grid getDataGrid() {
        if (dataGrid == null){
            dataGrid = new Grid(1,2);
        }
        return dataGrid;
    }
    public DisclosurePanel getDisclosurePanel() {
        if (disclosurePanel == null ){
            disclosurePanel = new DisclosurePanel("");
        }
        return disclosurePanel;
    }
    public void setTextEditPanel(Grid textEditPanel) {
        this.dataGrid = textEditPanel;
    }
}